/*
 * FindAnswer.cpp
 * Created on: 03/11/2010
 * Author: catharine
 */

#include "FindAnswer.h"

const double threshold1 = 70;

void verticalValues(int* verticalSizes, int* verticalInitial, IplImage* image) {
	int height = image->height;
	int counter = 0;
	for (int i = 0; i < 1; i++) {
		for (int j = 0; j < height; j++) {
			int size = 0;
			if (cvGetReal2D(image, j, i) == 0) {
				verticalInitial[counter] = j;
				while (cvGetReal2D(image, j, i) == 0) {
					j++;
					size++;
				}
				verticalSizes[counter] = size;
				counter++;
			}
		}
	}
}

void horizontalValues(int* horizontalSizes, int* horizontalInitial,
		IplImage* image) {
	int width = image->width;
	int counter = 0;
	for (int i = 0; i < 1; i++) {
		for (int j = 0; j < width; j++) {
			int size = 0;
			if (cvGetReal2D(image, i, j) == 0) {
				horizontalInitial[counter] = j;
				while (cvGetReal2D(image, i, j) == 0) {
					j++;
					size++;
				}
				horizontalSizes[counter] = size;
				counter++;
			}
		}
	}
}

double findNumberBlackPixels(IplImage* image, int height, int width) {
	double sum = 0;
	for (int i = 0; i < height; i++) {
		for (int j = 0; j < width; j++) {
			if (cvGetReal2D(image, i, j) == 0) {
				sum++;
			}
		}
	}
	return sum;
}

void findSumROI(double **sumROI, IplImage* image, int sizeV, int sizeH,
		int* horizontalInitial, int* horizontalSizes, int* verticalInitial,
		int* verticalSizes, int middle) {

	for (int i = 0; i < sizeH; i++) {
		for (int j = 0; j < sizeV; j++) {
			int width = horizontalSizes[i];
			int height = verticalSizes[j];
			cvSetImageROI(image, cvRect(horizontalInitial[i],
					verticalInitial[j], width, height));
			if (j < middle) {
				sumROI[i][j] = findNumberBlackPixels(image, height, width);
			} else {
				sumROI[i + 16][j - 5] = findNumberBlackPixels(image, height,
						width);
			}
			cvResetImageROI(image);
		}
	}
}

void createArchieve(String name, string* array, int arraySize) {
	ofstream arquivo;
	arquivo.open(name.c_str());

	for (int i = 0; i < arraySize; i++) {
		arquivo << array[i] << endl;
	}
	arquivo.close();
}

void createArchieve(string name, string content) {
	ofstream arquivo;
	arquivo.open(name.c_str());
	arquivo << content;
	arquivo.close();
}

string findAnswer(double** sumROI, int doublee, int middle) {
	stringstream s;
	s << "";
	for (int i = 0; i < doublee; i++) {
		s << (i + 1);
		int lengthBefore = s.str().length();
		for (int j = 0; j < middle; j++) {
			if (sumROI[i][j] >= threshold1) {
				if (j == 0) {
					s << "A";
				} else if (j == 1) {
					s << "B";
				} else if (j == 2) {
					s << "C";
				} else if (j == 3) {
					s << "D";
				} else {
					s << "E";
				}
			}
		}
		int lengthAfter = s.str().length();
		if (lengthAfter == lengthBefore) {
			s << "V";
		}
	}
	return s.str();
}

string fileName(int i) {
	stringstream name;
	name << "Imagens/resposta" << i;
	return name.str();
}

int main(int argc, char **argv) {
	int numberDocuments = 1;
	string content;
	for (int i = 1; i <= numberDocuments; i++) {
		string fName = fileName(i);
		string pic = fName;

		IplImage* image = cvLoadImage(pic.append(".bmp").c_str(),
				CV_LOAD_IMAGE_GRAYSCALE);

		const int sizeH = 16;
		const int sizeV = 10;
		const int doublee = sizeH * 2;
		const int middle = sizeV / 2;

		int* horizontalSizes = new int[sizeH];
		int* horizontalInitial = new int[sizeH];

		int* verticalSizes = new int[sizeV];
		int* verticalInitial = new int[sizeV];

		horizontalValues(horizontalSizes, horizontalInitial, image);
		verticalValues(verticalSizes, verticalInitial, image);

		double ** sumROI = new double*[doublee];
		for (int i = 0; i < doublee; i++) {
			sumROI[i] = new double[middle];
		}

		findSumROI(sumROI, image, sizeV, sizeH, horizontalInitial,
				horizontalSizes, verticalInitial, verticalSizes, middle);

		content.append(pic);
		content.append(" ");
		content.append(findAnswer(sumROI, doublee, middle));
		content.append("\n");

		cvReleaseImage(&image);
	}
	createArchieve("Respostas.txt", content);
	return 0;
}
